package cn.hs.ph.dao.impl;


import cn.hs.ph.dao.GoodsDao;
import cn.hs.ph.entity.Goods;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public int insert(Goods goods) {
        String sql="insert into goods values(null,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] obj={
                goods.getGoodsName(),
                goods.getGoodsImage(),
                goods.getGoodsPresentPrice(),
                goods.getShopId(),
                goods.getGoodsStatus(),
                goods.getShopCategoryId(),
                new Date(),
                goods.getGoodsUnit(),
                goods.getGoodsCartCount(),
                goods.getGoodsShopName(),
                goods.getGoodsSalesVolume()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int goodsId) {
        String sql="delete from goods where goodsId=?";
        Object[] obj={goodsId};
        int delete = JDBCDruidUtil.update(sql, obj);
        if(delete!=0){
            return delete;
        }else
        {
            return 0;
        }
    }

    @Override
    public int update(Goods goods) {
        String sql="update goods set goodsName=?,goodsImage=?,goodsPresentPrice=?,shopId=?,goodsStatus=?,shopCategoryId=?,addTime=?,goodsUnit=?,goodsCartCount=?,goodsShopName=?,goodsSalesVolume=? where goodsId=?";
        Object[] obj={
                goods.getGoodsName(),
                goods.getGoodsImage(),
                goods.getGoodsPresentPrice(),
                goods.getShopId(),
                goods.getGoodsStatus(),
                goods.getShopCategoryId(),
                new Date(),
                goods.getGoodsUnit(),
                goods.getGoodsCartCount(),
                goods.getGoodsShopName(),
                goods.getGoodsSalesVolume(),
                goods.getGoodsId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Goods> selectAll(Map<String, String> map) {
        List<Goods> list=new ArrayList<>();
        String sql="select * from goods where 1=1";
        List params=new ArrayList<>();
        String goodsName = map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        //模糊查询店名
        String goodsShopName = map.get("goodsShopName");
        if (goodsShopName!=null&&!"".equals(goodsShopName)){
            sql+=" and goodsShopName like ? ";
            params.add(goodsShopName);
        }
        //判断模糊查询的时间
        String startTime = map.get("startTime");
        if(startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //判断是否需要分页
        String pn = map.get("pageNum");
        if (pn!=null&&!"".equals(pn)){
            sql+=" limit ?,? ";
            int pageNum = Integer.parseInt(map.get("pageNum"));
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj=params.toArray();
        ResultSet res= JDBCDruidUtil.query(sql,obj);
        while (true){
            try {
                if (!res.next()) break;
                Goods goods=new Goods(
                res.getInt(1),
                res.getString(2),
                res.getString(3),
                res.getBigDecimal(4),
                res.getInt(5),
                res.getInt(6),
                res.getInt(7),
                res.getDate(8),
                res.getString(9),
                res.getString(10),
                res.getString(11),
                res.getString(12)
                );
                list.add(goods);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        String sql="select count(1) from goods where 1=1";
        List params=new ArrayList<>();
        String goodsName = map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        //判断模糊查询的时间
        String startTime = map.get("startTime");
        if(startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj=params.toArray();
        ResultSet res= JDBCDruidUtil.query(sql,obj);
        while (true){
            try {
                if (!res.next()) break;
                count=res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }

    @Override
    public int modify(Goods goods) {
        String sql="update goods set goodsStatus=? where goodsId=?";
        Object[] obj={goods.getGoodsStatus(),goods.getGoodsId()};
        int num = JDBCDruidUtil.update(sql, obj);
        return num;
    }
}
